﻿using System.Web.Mvc;
using MMSWebsite.DataAccessManagement;
using MMSWebsite.Models;

namespace MMSWebsite.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        RoleAccessProvider _roleProvider = new RoleAccessProvider();
        public ActionResult Index()
        {
            var roles = _roleProvider.GetAllRoles();
            return View(roles);
        }

        //
        // GET: /Role/Details/5

        public ActionResult Details(int id)
        {
            return View(_roleProvider.GetRoleById(id));
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            ViewBag.RoleExistedError = "";
            return View();
        }

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(MMS_ROLE role, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                if (!_roleProvider.CheckRoleExist(role.RoleName,false))
                {
                    _roleProvider.AddRole(role);
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.RoleExistedError = "Role name existed! Please try another name.";
                    return View(role);
                }
            }
            else
            {
                return View(role);
            }

        }

        //
        // GET: /Role/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.RoleExistedError = "";
            return View(_roleProvider.GetRoleById(id));
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit( MMS_ROLE role, int id, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                if (!_roleProvider.CheckRoleExist(role.RoleName, true))
                {
                    _roleProvider.UpdateRole(role);
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.RoleExistedError = "Role name existed! Please try another name.";
                    return View(role);
                }
            }
            else return View(role);
        }

        //
        // GET: /Role/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_roleProvider.GetRoleById(id));
        }

        //
        // POST: /Role/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                _roleProvider.RemoveRole(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
